<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.custormer" placeholder="客户名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getRepairOrders">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>


		<!--列表-->
		<el-table :data="repairOrders" highlight-current-row v-loading="listLoading"  @selection-change="selsChange" @row-dblclick="handle" style="width: 100%;">
			<el-table-column type="selection" width="55" align="center">
			</el-table-column>
			<el-table-column type="index" width="60" align="center">
			</el-table-column>
			<el-table-column prop="custormer" label="客户名称" sortable align="center">
			</el-table-column>
			<el-table-column prop="carNum" label="车牌号" sortable align="center">
			</el-table-column>
			<el-table-column prop="createTime" label="创建时间" sortable align="center">
			</el-table-column>
			<el-table-column prop="address" label="地址" sortable align="center">
			</el-table-column>
			<el-table-column prop="name" label="维修工" sortable align="center">
			</el-table-column>
			<el-table-column prop="status" label="状态" :formatter="statusFormat" sortable align="center">
			</el-table-column>
			<el-table-column label="操作" width="150" align="center">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
			</el-pagination>
		</el-col>


		<!--结算单弹出框-->
		<el-dialog title="结算单" :visible.sync="dialogTableVisible1" >
			<el-form :model="settedForm" label-width="80px" :rules="editFormRules" ref="settedForm">
				<el-form-item label="顾客名称" prop="custormer">
					<el-input v-model="settedForm.custormer" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="地址" prop="address">
					<el-input v-model="settedForm.address" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="应付金额" prop="re_amount">
					<el-input v-model="settedForm.re_amount" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="实付金额" prop="pay_amount">
					<el-input v-model="settedForm.pay_amount" auto-complete="off"></el-input>
				</el-form-item>

				<el-form-item label="支付方式" prop="opt_id">
					<template>
						<el-select v-model="settedForm.pay_id" placeholder="请选择">
							<el-option
									v-for="item in types"
									:key="item.id"
									:label="item.typeName"
									:value="item.id">
							</el-option>
						</el-select>
					</template>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="dialogTableVisible1 = false">取消</el-button>
				<el-button type="primary" @click.native="setSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>
		<!--维修单明细弹出框-->
		<el-dialog title="维修单明细" :visible.sync="dialogTableVisible" >
			<el-button type="primary" @click="handleAdd1">新增</el-button>
			<el-button type="success" @click="handleSettle">结算</el-button>
			<el-table :data="repairOrderItems">
				<el-table-column property="partName" label="配件" width="100px"align="center">
                </el-table-column>
				<el-table-column property="amt1" label="配件价格" align="center"></el-table-column>
				<el-table-column property="num" label="配件数量" align="center"></el-table-column>
				<el-table-column property="amt2" label="工时费"  align="center"></el-table-column>
				<el-table-column property="name" label="维修工" align="center"></el-table-column>
				<el-table-column property="totalAmt" label="总金额" align="center"></el-table-column>
				<el-table-column property="order_id" label="订单编号" hidden="hidden" align="center"></el-table-column>
				<el-table-column label="操作" width="150" align="center">
					<template scope="scope">
						<el-button size="small" @click="handleEdit1(scope.$index, scope.row)">编辑</el-button>
						<el-button type="danger" size="small" @click="handleDel1(scope.$index, scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-col :span="24" class="toolbar">
				<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange1" :page-size="10" :total="total" style="float:right;">
				</el-pagination>
			</el-col>
		</el-dialog>
		<!--订单编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="客户名称" prop="custormer">
					<el-input v-model="editForm.custormer" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="车牌号" prop="carNum">
					<el-input v-model="editForm.carNum" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="地址" prop="address">
					<el-input v-model="editForm.address" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="维修工" prop="opt_id">
					<template>
						<el-select v-model="editForm.opt_id" placeholder="请选择">
							<el-option
									v-for="item in opts"
									:key="item.id"
									:label="item.name"
									:value="item.id">
							</el-option>
						</el-select>
					</template>
				</el-form-item>
				<el-form-item label="状态" prop="status">
					<template>
						<el-select v-model="editForm.status" placeholder="请选择">
							<el-option
									v-for="item in options"
									:key="item.value"
									:label="item.label"
									:value="item.value">
							</el-option>
						</el-select>
					</template>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--订单新增界面-->
		<el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="客户名称" prop="custormer">
					<el-input v-model="addForm.custormer" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="id" prop="id" hidden="hidden">
					<el-input v-model="addForm.id" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="车牌号" prop="carNum">
					<el-input v-model="addForm.carNum" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="地址" prop="address">
					<el-input v-model="addForm.address" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="维修工" prop="opt_id">
					<template>
						<el-select v-model="addForm.opt_id" placeholder="请选择">
							<el-option
									v-for="item in opts"
									:key="item.id"
									:label="item.name"
									:value="item.id">
							</el-option>
						</el-select>
					</template>
				</el-form-item>
				<el-form-item label="状态" prop="status">
					<template>
						<el-select v-model="addForm.status" placeholder="请选择">
							<el-option
									v-for="item in options"
									:key="item.value"
									:label="item.label"
									:value="item.value">
							</el-option>
						</el-select>
					</template>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
		<!--订单明细新增界面-->
		<el-dialog title="新增" :visible.sync="addFormVisible1" :close-on-click-modal="false">
			<el-form :model="addForm1" label-width="80px" :rules="addFormRules" ref="addForm1">
				<el-form-item label="配件" prop="parts_id">
                    <template>
                        <el-select v-model="addForm1.parts_id" placeholder="请选择">
                            <el-option
                                    v-for="opt in parts"
                                    :key="opt.id"
                                    :label="opt.name"
                                    :value="opt.id">
                            </el-option>
                        </el-select>
                    </template>
				</el-form-item>
				<el-form-item label="订单号" prop="order_id" hidden="hidden">
					<el-input v-model="addForm1.order_id" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="配件价格" prop="amt1" >
					<el-input v-model="addForm1.amt1" auto-complete="off" :formatter="partsFormat"></el-input>
				</el-form-item>
				<el-form-item label="配件数量" prop="num">
					<el-input v-model="addForm1.num" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="工时费" prop="amt2">
					<el-input v-model="addForm1.amt2" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="维修工" prop="opt_id">
					<template>
						<el-select v-model="addForm1.opt_id" placeholder="请选择">
							<el-option
									v-for="opt in opts"
									:key="opt.id"
									:label="opt.name"
									:value="opt.id">
							</el-option>
						</el-select>
					</template>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible1 = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit1" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>

    export default {
        data() {
            return {
                //明细单弹出框状态
                dialogTableVisible: false,
				//结算单弹出框状态
                dialogTableVisible1:false,
                dialogTableVisible2:false,
				amts:0,
				//状态的下拉列表数据
                options: [{
                    value: false,
                    label: '未结算'
                }, {
                    value: true,
                    label: '已结算'
                }],value: '',
				//维修工
                opts: [],
				//支付方式
				types:[],
                parts:[
                ],
                //高级查询时传递的值
                filters:{
                    custormer:''
                },
                currentPage:1,
                total:0,
                sels: [],//列表选中列
                repairOrders: [],
                itemForm:{
                    order_id:'',
					address:'',
					custormer:'',
					carNum:'',
					createTime:'',
					address:'',
					opt_id:''
                },
                repairOrderItems: [],
                listLoading: false,//如果为false，显示加载中效果，转圈圈
                editFormVisible: false,//编辑界面是否显示，false不显示
                //编辑界面数据,修改form对象
                editForm: {
                    id:0,
                    name:''
                },
                editForm1: {
                    id:0,
                    name:''
                },
				settedForm:{
                    re_amount:0,
					pay_amount:0,
					address:'',
					custormer:'',
					order_id:1,
					pay_id:1,
				},
				updateForm:{},
                editLoading: false,//修改加载
                editFormRules: {
                    custormer: [
                        { required: true, message: '请输入姓名', trigger: 'blur' }
                    ],
                    carNum: [
                        { required: true, message: '请输入车牌号', trigger: 'blur' }
                    ],
                    address: [
                        { required: true, message: '请输入地址', trigger: 'blur' }
                    ]
                },
                addFormVisible: false,//新增界面是否显示
                addFormVisible1: false,//新增界面是否显示
                //新增界面数据，修改form对象
                addForm: {
                },
                addForm1: {
                    order_id:''
                },
                addLoading: false,//新增加载
                addFormRules: {
                    custormer: [
                        { required: true, message: '请输入姓名', trigger: 'blur' }
                    ],
                    carNum: [
                        { required: true, message: '请输入车牌号', trigger: 'blur' }
                    ],
                    address: [
                        { required: true, message: '请输入地址', trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            //状态的格式处理
            statusFormat(row,column){
                var s = row.status;
                if(s){
                    return "已结算"
				}else{
                    return "未结算"
				}
			},
            partsFormat(row){
                if(this.addForm1){
                    return
                }
            },
            //获取维修单列表
            handleCurrentChange(val){
                //当前页
                this.currentPage = val;
                this.getRepairOrders();
            },
			//获取维修单明细列表
            handleCurrentChange1(val){
                //当前页
                this.currentPage = val;
                this.getRepairOrderItems();
            },
			getAmt(){
                let param = {
                   id:1
                }
                this.$http.get('/repairOrderItem/getAmt',param).then(res =>{
                    //pageList {total:20,rows:[]}
					console.log(res.data)
                })
			},
            getRepairOrders() {
                //发送axios请求到后端 查询维修单
                // http://localhost:80/repairOrder/list
                let param = {
                    custormer:this.filters.custormer,
                    currentPage: this.currentPage,
                }
                this.$http.patch('/repairOrder/queryPage',param).then(res =>{
                    //pageList {total:20,rows:[]}
                    this.total = res.data.total;
                    this.repairOrders = res.data.rows;
                })
            },
            getRepairOrderItems() {
                //发送axios请求到后端 查询订单明细
                // http://localhost:80/department/list
                this.addForm1.order_id=this.itemForm.order_id;
                console.log("22222"+this.itemForm.address)
                let param = {
                    //对应的订单id
                    order_id:this.itemForm.order_id,
                    currentPage: this.currentPage,
                }
                this.$http.patch('/repairOrderItem/queryPage',param).then(res =>{
                    //pageList {total:20,rows:[]}
                    this.total = res.data.total;
                    this.repairOrderItems = res.data.rows;
                    let arr=res.data.rows;
                    //总金额
                    let amt = 0;
					for(let i=0;i<arr.length;i++){
                        amt=amt+arr[i].totalAmt;
					}
					this.amts=amt;
					this.settedForm.re_amount=amt;
                })
            },
            getOpts() {
                //发送axios请求到后端 查询维修工
                this.$http.patch('/opt/list').then(res =>{
                    this.opts = res.data;
                })
            },
			getTypes(){
                this.$http.patch('/payType/list').then(res =>{
                    this.types = res.data;
                })
			},
			getParts(){
                this.$http.patch('/parts/findAll').then(res =>{
                    this.parts = res.data;
                })
			},
            //弹出新增界面
            handleAdd() {
                this.addFormVisible = true;
            },
			//弹出订单明细新增窗口
            handleAdd1() {
                this.addFormVisible1 = true;

            },
            handleSettle(){
                this.dialogTableVisible1 = true;

			},
            handle(val){
                this.itemForm.order_id=val.id;
                this.updateForm = val;
                console.log(this.updateForm);
                this.settedForm.address=val.address;
                this.settedForm.custormer=val.custormer;
                this.settedForm.order_id=val.id;
                this.getRepairOrderItems();
               this.dialogTableVisible = true;
            },
            //弹出编辑界面
            handleEdit(index, row) {
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
            },
            //弹出编辑界面
            handleEdit1(index, row) {
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
            },
            //删除
            handleDel(index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    //出现加载中效果
                    this.listLoading = true;
                    let id = row.id;
                    this.$http.delete('repairOrder/'+id).then(res=>{
                        //去掉转圈圈效果
                        this.listLoading = false;
                        let {success,message} = res.data;
                        if(success){
                            this.$message({
                                message: message,
                                type: 'success'
                            });
                            this.getRepairOrders();
                        }else {
                            this.$message({
                                message: message,
                                type: 'error'
                            });
                        }
                    });
                }).catch(() => {

                });
            },
            //删除
            handleDel1(index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    //出现加载中效果
                    this.listLoading = true;
                    let id = row.id;
                    this.$http.delete('repairOrderItem/'+id).then(res=>{
                        //去掉转圈圈效果
                        this.listLoading = false;
                        let {success,message} = res.data;
                        if(success){
                            this.$message({
                                message: message,
                                type: 'success'
                            });
                            this.getRepairOrderItems();
                            this.dialogTableVisible.reload();
                        }else {
                            this.$message({
                                message: message,
                                type: 'error'
                            });
                        }
                    });
                }).catch(() => {

                });
            },
            //新增
            addSubmit() {
                //提交之前的验证工作--elementui验证
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            //出现加载中效果
                            this.addLoading = true;
                            //{name:"xxx"}把新增表单里面form 拷贝到 {}
                            let para = Object.assign({}, this.addForm);
                            this.$http.put('repairOrder/save',para).then((res) => {
                                //去掉转圈圈效果
                                this.addLoading = false;
                                let {success,message} = res.data;
                                if(success){
                                    this.$message({
                                        message: message,
                                        type: 'success'
                                    });
                                    //清空表单
                                    this.$refs['addForm'].resetFields();
                                    //关闭对话框
                                    this.addFormVisible = false;
                                    this.getRepairOrders();
                                }else {
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }
                            });
                        });
                    }
                });
            },
            addSubmit1() {
                //提交之前的验证工作--elementui验证
                this.$refs.addForm1.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            //出现加载中效果
                            this.addLoading = true;
                            //{name:"xxx"}把新增表单里面form 拷贝到 {}
                            let para = Object.assign({}, this.addForm1);
                            this.$http.put('repairOrderItem/save',para).then((res) => {
                                //去掉转圈圈效果
                                this.addLoading = false;
                                let {success,message} = res.data;
                                if(success){
                                    this.$message({
                                        message: message,
                                        type: 'success'
                                    });
                                    //清空表单
                                    this.$refs['addForm1'].resetFields();
                                    //关闭对话框
                                    this.addFormVisible1 = false;
                                    this.getRepairOrderItems();
                                }else {
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }
                            });
                        });
                    }
                });
            },
            setSubmit() {
                //提交之前的验证工作--elementui验证
                this.$refs.settedForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            //出现加载中效果
                            this.addLoading = true;
                            //{name:"xxx"}把新增表单里面form 拷贝到 {}
                            let para = Object.assign({}, this.settedForm);
                            this.$http.put('setted/save',para).then((res) => {
                                //去掉转圈圈效果
                                this.addLoading = false;
                                let {success,message} = res.data;
                                if(success){
                                    this.$message({
                                        message: message,
                                        type: 'success'
                                    });
                                    //清空表单
                                    this.$refs['settedForm'].resetFields();

                                    //关闭对话框
                                    this.dialogTableVisible1 = false;
                                    let status = Object.assign({}, this.updateForm);
                                    console.debug("=======")
                                    console.debug(status)
                                    this.$http.post('/repairOrder/update1',status).then(res =>{
                                    })
                                    this.getRepairOrderItems();
                                    this.getRepairOrders();
                                }else {
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }
                            });
                        });
                    }
                });
            },
            //修改提交
            editSubmit() {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            let para = Object.assign({}, this.editForm);
                            this.$http.post('repairOrder/update',para).then((res) => {
                                this.editLoading = false;
                                let {success,message} = res.data;
                                if(success){
                                    this.$message({
                                        message: message,
                                        type: 'success'
                                    });
                                    this.$refs['editForm'].resetFields();
                                    this.editFormVisible = false;
                                    this.getRepairOrders();
                                }else{
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }
                            });
                        });
                    }
                });
            },
            //修改提交
            editSubmit1() {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            let para = Object.assign({}, this.editForm1);
                            this.$http.post('repairOrderItem/update',para).then((res) => {
                                this.editLoading = false;
                                let {success,message} = res.data;
                                if(success){
                                    this.$message({
                                        message: message,
                                        type: 'success'
                                    });
                                    this.$refs['editForm'].resetFields();
                                    this.editFormVisible = false;
                                    this.getRepairOrders();
                                }else{
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }
                            });
                        });
                    }
                });
            },
            //多选方法
            selsChange: function (sels) {
                this.sels = sels;
            }
        },
        mounted() {
            this.getRepairOrders();
            this.getOpts();
            this.getTypes();
            this.getParts();
        }
    }

</script>

<style scoped>

</style>